MongoDB 集群构建:分片+副本+选举
❤️ 环境准备
三台服务器,建立 Docker Swarm 集群,一个 Manager(node1),两个 Worker(node2 + node3)。
docker 版本:19.03.0-beta1
mongo 版本:4.0.3
❤️ MongoDB 集群架构设计
架构图
高清图地址: https://www.processon.com/view/link/5a3c7386e4b0bf89b8530376
❤️ 搭建集群
1、【Manager】创建集群网络
1docker network create -d overlay --attachable mongo
--attachable 允许其他容器加入此网络
2、创建 9 个 Data 服务,3 个 Config 服务,1 个 Global 模式的 Mongos 服务
2.1、【所有机器】创建相关文件夹
1mkdir -p ~/mongo/config ~/mongo/shard1 ~/mongo/shard2 ~/mongo/shard3
2.2、【Manager】创建 stack.yml
1version: '3.7'
2services:
3 mongors1n1:
4 image: mongo
5 command: mongod --shardsvr --replSet shard1 --dbpath /data/db --bind_ip_all --port 27017
6 networks:
7 - mongo
8 environment:
9 - TZ=Asia/Shanghai
10 volumes:
11 - ~/mongo/shard1:/data/db
12 deploy:
13 replicas: 1
14 placement:
15 constraints:
16 - node.hostname==node1
17 mongors2n1:
18 image: mongo
19 command: mongod --shardsvr --replSet shard2 --dbpath /data/db --bind_ip_all --port 27017
20 networks:
21 - mongo
22 environment:
23 - TZ=Asia/Shanghai
24 volumes:
25 - ~/mongo/shard2:/data/db
26 deploy:
27 replicas: 1
28 placement:
29 constraints:
30 - node.hostname==node1
31 mongors3n1:
32 image: mongo
33 command: mongod --shardsvr --replSet shard3 --dbpath /data/db --bind_ip_all --port 27017
34 networks:
35 - mongo
36 environment:
37 - TZ=Asia/Shanghai
38 volumes:
39 - ~/mongo/shard3:/data/db
40 deploy:
41 replicas: 1
42 placement:
43 constraints:
44 - node.hostname==node1
45 mongors1n2:
46 image: mongo
47 command: mongod --shardsvr --replSet shard1 --dbpath /data/db --bind_ip_all --port 27017
48 networks:
49 - mongo
50 environment:
51 - TZ=Asia/Shanghai
52 volumes:
53 - ~/mongo/shard1:/data/db
54 deploy:
55 replicas: 1
56 placement:
57 constraints:
58 - node.hostname==node2
59 mongors2n2:
60 image: mongo
61 command: mongod --shardsvr --replSet shard2 --dbpath /data/db --bind_ip_all --port 27017
62 networks:
63 - mongo
64 environment:
65 - TZ=Asia/Shanghai
66 volumes:
67 - ~/mongo/shard2:/data/db
68 deploy:
69 replicas: 1
70 placement:
71 constraints:
72 - node.hostname==node2
73 mongors3n2:
74 image: mongo
75 command: mongod --shardsvr --replSet shard3 --dbpath /data/db --bind_ip_all --port 27017
76 networks:
77 - mongo
78 environment:
79 - TZ=Asia/Shanghai
80 volumes:
81 - ~/mongo/shard3:/data/db
82 deploy:
83 replicas: 1
84 placement:
85 constraints:
86 - node.hostname==node2
87 mongors1n3:
88 image: mongo
89 command: mongod --shardsvr --replSet shard1 --dbpath /data/db --bind_ip_all --port 27017
90 networks:
91 - mongo
92 environment:
93 - TZ=Asia/Shanghai
94 volumes:
95 - ~/mongo/shard1:/data/db
96 deploy:
97 replicas: 1
98 placement:
99 constraints:
100 - node.hostname==node3
101 mongors2n3:
102 image: mongo
103 command: mongod --shardsvr --replSet shard2 --dbpath /data/db --bind_ip_all --port 27017
104 networks:
105 - mongo
106 environment:
107 - TZ=Asia/Shanghai
108 volumes:
109 - ~/mongo/shard2:/data/db
110 deploy:
111 replicas: 1
112 placement:
113 constraints:
114 - node.hostname==node3
115 mongors3n3:
116 image: mongo
117 command: mongod --shardsvr --replSet shard3 --dbpath /data/db --bind_ip_all --port 27017
118 networks:
119 - mongo
120 environment:
121 - TZ=Asia/Shanghai
122 volumes:
123 - ~/mongo/shard3:/data/db
124 deploy:
125 replicas: 1
126 placement:
127 constraints:
128 - node.hostname==node3
129 cfg1:
130 image: mongo
131 command: mongod --configsvr --replSet cfgrs --dbpath /data/db --bind_ip_all --port 27017
132 networks:
133 - mongo
134 environment:
135 - TZ=Asia/Shanghai
136 volumes:
137 - ~/mongo/config:/data/db
138 deploy:
139 replicas: 1
140 placement:
141 constraints:
142 - node.hostname==node1
143 cfg2:
144 image: mongo
145 command: mongod --configsvr --replSet cfgrs --dbpath /data/db --bind_ip_all --port 27017
146 networks:
147 - mongo
148 environment:
149 - TZ=Asia/Shanghai
150 volumes:
151 - ~/mongo/config:/data/db
152 deploy:
153 replicas: 1
154 placement:
155 constraints:
156 - node.hostname==node2
157 cfg3:
158 image: mongo
159 command: mongod --configsvr --replSet cfgrs --dbpath /data/db --bind_ip_all --port 27017
160 networks:
161 - mongo
162 environment:
163 - TZ=Asia/Shanghai
164 volumes:
165 - ~/mongo/config:/data/db
166 deploy:
167 replicas: 1
168 placement:
169 constraints:
170 - node.hostname==node3
171 mongos:
172 image: mongo
173 command: mongos --configdb cfgrs/cfg1:27017,cfg2:27017,cfg3:27017 --bind_ip_all --port 27017
174 networks:
175 - mongo
176 ports:
177 - 27017:27017
178 environment:
179 - TZ=Asia/Shanghai
180 deploy:
181 mode: global
182networks:
183 mongo:
184 external: true
2.3、启动服务,在 Manager 上执行
1docker stack deploy -c stack.yml mongo
2.4、【Manager】查看服务的启动情况
1docker service ls
正常情况下,会出现如下结果:
1[docker@manager ~]# docker service ls
2ID NAME MODE REPLICAS IMAGE PORTS
3z1l5zlghlfbi mongo_cfg1 replicated 1/1 registry.docker-cn.com/library/mongo:latest
4lg9vbods29th mongo_cfg2 replicated 1/1 registry.docker-cn.com/library/mongo:latest
5i6d6zwxsq0ss mongo_cfg3 replicated 1/1 registry.docker-cn.com/library/mongo:latest
6o0lfdavd8kpj mongo_mongors1n1 replicated 1/1 registry.docker-cn.com/library/mongo:latest
7n85yeyod7mlu mongo_mongors1n2 replicated 1/1 registry.docker-cn.com/library/mongo:latest
8cwurdqng9tdk mongo_mongors1n3 replicated 1/1 registry.docker-cn.com/library/mongo:latest
9vu6al5kys28u mongo_mongors2n1 replicated 1/1 registry.docker-cn.com/library/mongo:latest
10xrjiep0vrf0w mongo_mongors2n2 replicated 1/1 registry.docker-cn.com/library/mongo:latest
11qqzifwcejjyk mongo_mongors2n3 replicated 1/1 registry.docker-cn.com/library/mongo:latest
12tddgw8hygv1b mongo_mongors3n1 replicated 1/1 registry.docker-cn.com/library/mongo:latest
13qrb6fjty03mw mongo_mongors3n2 replicated 1/1 registry.docker-cn.com/library/mongo:latest
14m8ikdzjssmhn mongo_mongors3n3 replicated 1/1 registry.docker-cn.com/library/mongo:latest
15mnnlm49b7kyb mongo_mongos global 3/3 registry.docker-cn.com/library/mongo:latest *:27017->27017/tcp
3、初始化集群
3.1 【node1】初始化 Mongo 配置集群
1docker exec -it $(docker ps | grep "cfg1" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id: \"cfgrs\",configsvr: true, members: [{ _id : 0, host : \"cfg1\" },{ _id : 1, host : \"cfg2\" }, { _id : 2, host : \"cfg3\" }]})' | mongo"
3.2 【node1】初始化三个 Mongo 数据集群
1docker exec -it $(docker ps | grep "mongors1n1" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id : \"shard1\", members: [{ _id : 0, host : \"mongors1n1\" },{ _id : 1, host : \"mongors1n2\" },{ _id : 2, host : \"mongors1n3\"}]})' | mongo"
2
3docker exec -it $(docker ps | grep "mongors2n1" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id : \"shard2\", members: [{ _id : 0, host : \"mongors2n1\" },{ _id : 1, host : \"mongors2n2\" },{ _id : 2, host : \"mongors2n3\" }]})' | mongo"
4
5docker exec -it $(docker ps | grep "mongors3n1" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id : \"shard3\", members: [{ _id : 0, host : \"mongors3n1\" },{ _id : 1, host : \"mongors3n2\" },{ _id : 2, host : \"mongors3n3\" }]})' | mongo"
3.3 【node1】将三个数据集群当做分片加入 mongos
1docker exec -it $(docker ps | grep "mongos" | awk '{ print $1 }') bash -c "echo 'sh.addShard(\"shard1/mongors1n1:27017,mongors1n2:27017,mongors1n3:27017\")' | mongo "
2
3docker exec -it $(docker ps | grep "mongos" | awk '{ print $1 }') bash -c "echo 'sh.addShard(\"shard2/mongors2n1:27017,mongors2n2:27017,mongors2n3:27017\")' | mongo "
4
5docker exec -it $(docker ps | grep "mongos" | awk '{ print $1 }') bash -c "echo 'sh.addShard(\"shard3/mongors3n1:27017,mongors3n2:27017,mongors3n3:27017\")' | mongo "
4、连接集群
4.1 内部:在 mongo 网络下的容器,通过 mongos:27017
连接
4.2 外部:通过 IP:27017
连接,IP 可以为三台服务的中的一个的 IP
5、测试数据
1# 【node1】连接集群
2docker exec -it $(docker ps | grep "mongos" | awk '{ print $1 }') mongo mongos:27017
3
4# 【node1】创建数据库
5use test_db
6
7# 【node1】允许数据库分片
8sh.enableSharding("test_db", "hashed")
9
10# 【node1】创建文档
11db.createCollection("user")
12
13# 【node1】分片设置
14sh.shardCollection( "test_db.user", {_id:"hashed"} )
15
16# 【node1】添加数据
17for(var i = 0; i < 1000; i++) { db.user.insert({_id:i, name:"test" + i}) }
18
19# 【node1】查询数据
20db.user.find().pretty()
21
22# 【node1】查询分片状态
23db.user.getShardDistribution()
https://docs.mongodb.com/manual/core/ranged-sharding
https://docs.mongodb.com/manual/core/hashed-sharding/
❤️ 相关问题
此处记录相关问题,欢迎提问!
● 如何设计缓存系统:缓存穿透,缓存击穿,缓存雪崩解决方案分析
● Spring Boot实战:逐行释义HelloWorld
● Spring Boot的自动配置、Command-line Runner
● Spring Boot 应用 - 静态视频资源实时播放新姿势